{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5d6f6161",
   "metadata": {},
   "source": [
    "* 2023-11-13\n",
    "* python课程\n",
    "* week11"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd583fff",
   "metadata": {},
   "source": [
    "# 1.元组 Tuples\n",
    "> 1.1 概念   \n",
    "> 1.2 创建   \n",
    "> 1.3 一般方法  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6478ef02",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 列表可以存放不同的数据，但列表一般情况下存储相同数字特征的数据，而元组相反。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f26a9c10",
   "metadata": {},
   "source": [
    "1. 有序性ordered\n",
    "2. 不可变性 unchangable\n",
    "3. 异构性 Heterogenrous\n",
    "4. 可重复性 duplicate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cea7dfee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1699873696.6639926"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import time\n",
    "time.time()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "037bcc6e",
   "metadata": {},
   "source": [
    "### 2.创建方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "edf97ed9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第一种\n",
    "my_tuple1 = 1699873696,100001,\"小明\",18,\"冰岛\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "618da740",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1699873696, 100001, '小明', 18, '冰岛')\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "print(my_tuple1)\n",
    "print(type(my_tuple1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7dd2c205",
   "metadata": {},
   "outputs": [],
   "source": [
    "my_tuple2 = (1699873696,100001,\"小明\",18,\"冰岛\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e871dd18",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1699873696, 100001, '小明', 18, '冰岛')\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "print(my_tuple2)\n",
    "print(type(my_tuple2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "62b723a2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第二种\n",
    "my_tuple3 = tuple((1699873696,100001,\"小明\",18,\"冰岛\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "60d77a0a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1699873696, 100001, '小明', 18, '冰岛')\n",
      "<class 'tuple'>\n"
     ]
    }
   ],
   "source": [
    "print(my_tuple3)\n",
    "print(type(my_tuple3))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6ef3909",
   "metadata": {},
   "source": [
    "## 3 元组的一般方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff017e2a",
   "metadata": {},
   "source": [
    "* 关于序列数据的一般方法均适用于元组\n",
    "> 1.indexing and slicing（索引取值和切片）   \n",
    "> 2.in 方法查询   \n",
    "> 3.len()  \n",
    "> 4.for 循环遍历   \n",
    "> 5.元组独有的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1c7bd986",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "18"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_tuple3[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3bd85314",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('小明', 18, '冰岛')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_tuple3[2:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "035b7dc4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"小明\" in my_tuple3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "bad8e2e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"李华\"in my_tuple3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "af6c4a24",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(my_tuple3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "feab765b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1699873696\n",
      "100001\n",
      "小明\n",
      "18\n",
      "冰岛\n"
     ]
    }
   ],
   "source": [
    "# print的缩进有讲究！！！\n",
    "for i in my_tuple3:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "cc987c67",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_tuple3.count(\"小明\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "ba5cf2cd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_tuple3.index(100001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "04a917ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 检验元组是否可以改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "deead7a3",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "'tuple' object does not support item assignment",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[34], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m my_tuple3[\u001b[38;5;241m0\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmery\u001b[39m\u001b[38;5;124m\"\u001b[39m\n",
      "\u001b[1;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
     ]
    }
   ],
   "source": [
    "my_tuple3[0] = \"mery\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ad70675",
   "metadata": {},
   "source": [
    "## 2.集合"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bd0de41",
   "metadata": {},
   "source": [
    "* 集合不能查询数量（count），因为都是唯一的，不会重复\n",
    "1. 无序性 unordered\n",
    "2. 不可变性 unchangable\n",
    "3. 异构性 Heterogenrous\n",
    "4. 独特性 unique"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "fd493109",
   "metadata": {},
   "outputs": [],
   "source": [
    "歌剧社 = {\"小明\",\"小红\",\"李华\",\"张三\"}\n",
    "街舞社 = {\"小明\",\"李鹏\",\"王芳\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "af70ea52",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小明', '小红', '张三', '李华', '李鹏', '王芳'}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 并集：去除重复项\n",
    "歌剧社.union(街舞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "fd4d3bbb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小明', '小红', '张三', '李华', '李鹏', '王芳'}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 运算法则\n",
    "歌剧社 | 街舞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "f9c044aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小明'}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 交集：找到重复项\n",
    "歌剧社.intersection(街舞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "ffa60a54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小明'}"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "歌剧社 & 街舞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "adc83e38",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小红', '张三', '李华'}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 差集：排除重复项，留下所选集合的单一项\n",
    "歌剧社.difference(街舞社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "5d620f6d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小红', '张三', '李华'}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "歌剧社 - 街舞社"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "aa42135b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'李鹏', '王芳'}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "街舞社.difference(歌剧社)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "e400882f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'小红', '张三', '李华', '李鹏', '王芳'}"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 对称差集:排掉不是唯一的\n",
    "歌剧社.symmetric_difference(街舞社)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3ac1d514",
   "metadata": {},
   "source": [
    "## 3.字典 dictionary\n",
    "{}\n",
    "> 1.无序性without any index value,the keys are their index    \n",
    "> 2.独特性keys   \n",
    "> 3.可修改value  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "917b4dab",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 字符串和数字还有元组可以作为keys（键），不像序列（用下标找数值），而是用keys（唯一的值），"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "a8a3c0d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "info = {\n",
    "    \"stu2201\":\"Ziqi\",\n",
    "    \"stu2202\":\"XiaoFeng\",\n",
    "    }\n",
    "# 学号作为keys（左边）（不可变），值(Value)在右边"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "3524e354",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu2201': 'Ziqi', 'stu2202': 'XiaoFeng'}"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "bc2f91bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "print(type(info))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e5116c0",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(info[\"stu2201\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "0f2fe31b",
   "metadata": {},
   "outputs": [],
   "source": [
    "info_01 = {\n",
    "    \"stu2202\":[\"Ziqi\",2200001,18,\"女\"],\n",
    "    \"stu2202\":[\"XiaoFeng\",2200002,0,\"男\"],\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "33980910",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu2202': ['XiaoFeng', 2200002, 0, '男']}"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info_01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad2243f1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
